<?php
/**
 * Created by PhpStorm.
 * User: ruma
 * Date: 2019/1/21
 * Time: 11:03
 */

namespace app\admin\model;


use app\admin\validate\LoginValidate;
use app\common\model\AdminBaseModel;
use app\Excepiton\ParameterException;
use think\Cookie;

class Login extends AdminBaseModel
{
    protected $table = 'admin';

    public function checkAdmin(){
        //1.调用通用的验证器并获取登陆信息
        $postData = (new LoginValidate())->postCheckAndGet();
        $adminName = $postData['admin_username'];

        //2.校验是否有该用户,无抛出异常,有验证密码
//        halt($adminName);
        $userInfo = $this->getUserInfoByUsername($adminName);
        $userInfo = $this->where('username','=',$adminName)->find();
        if (!$userInfo){
            throw new ParameterException([
                'msg' => '该用户不存在'
            ]);
        }
        if (strcmp($adminName,$userInfo['username']) !== 0){
            throw new ParameterException([
                'msg' => '该用户不存在'
            ]);
        }
        //3.用户信息存入session中
        session('admin_name',$userInfo['username']);
        session('admin_id',$userInfo['id']);

        return true;
    }
}